package com.fanxuankai.boot.mqbroker;

import com.fanxuankai.boot.mqbroker.autoconfigure.DingTalk;
import com.fanxuankai.boot.mqbroker.autoconfigure.MqBrokerProperties;
import com.fanxuankai.commons.util.DingTalkClientHelper;

/**
 * @author fanxuankai
 */
public class MqBrokerDingTalkClientHelper {
    private final MqBrokerProperties properties;
    private final DingTalkClientHelper dingTalkClientHelper;

    public MqBrokerDingTalkClientHelper(MqBrokerProperties properties) {
        this.properties = properties;
        DingTalk dingTalk = properties.getDingTalk();
        this.dingTalkClientHelper = DingTalkClientHelper.newInstance(dingTalk.getUrl(), dingTalk.getAccessToken(),
                dingTalk.getSecret());
    }

    public void push(String title, String topic, String code) {
        if (!properties.getDingTalk().isEnabled()) {
            return;
        }
        dingTalkClientHelper.sendRobotMarkdown(title, "# " + title + "\n" +
                        "> topic: " + topic + "\n\n" +
                        "> code: " + "<font color=#00B853>" + code + "</font>" + "\n\n" +
                        "> env: " + properties.getDingTalk().getEnv() + "\n\n",
                properties.getDingTalk().getAt().get(topic));
    }

    public void push(String title, String topic, String code, int retry, String ip) {
        if (!properties.getDingTalk().isEnabled()) {
            return;
        }
        dingTalkClientHelper.sendRobotMarkdown(title, "# " + title + "\n" +
                        "> topic: " + topic + "\n\n" +
                        "> code: " + "<font color=#00B853>" + code + "</font>" + "\n\n" +
                        "> retry: " + retry + "\n\n" +
                        "> ip: " + ip + "\n\n" +
                        "> env: " + properties.getDingTalk().getEnv() + "\n\n",
                properties.getDingTalk().getAt().get(topic));
    }

    public void push(String title, String topic, String code, long time, String ip) {
        if (!properties.getDingTalk().isEnabled()) {
            return;
        }
        dingTalkClientHelper.sendRobotMarkdown(title, "# " + title + "\n" +
                        "> topic: " + topic + "\n\n" +
                        "> code: " + "<font color=#00B853>" + code + "</font>" + "\n\n" +
                        "> time: " + time + "ms\n\n" +
                        "> ip: " + ip + "\n\n" +
                        "> env: " + properties.getDingTalk().getEnv() + "\n\n",
                properties.getDingTalk().getAt().get(topic));
    }

    public DingTalkClientHelper getDingTalkClientHelper() {
        return dingTalkClientHelper;
    }
}
